package cn.gailvlun.gll.chatlib.bean;

import java.util.Date;
import java.util.UUID;

import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.net.cross.CrossQuestionInfo;
import cn.gailvlun.gll.net.cross.CrossRoomInfo;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.Required;

public class CrossMessage extends RealmObject {
    @PrimaryKey
    private String id = UUID.randomUUID().toString();
    private int oppositeId;
    private Profile sender;
    private String type;
    private String text;
    private String image;
    private String localImage;
    @Required
    private Date created;
    private CrossRoomInfo crossRoomInfo;
    private int round;

    private int width;
    private int height;

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getOppositeId() {
        return oppositeId;
    }

    public void setOppositeId(int oppositeId) {
        this.oppositeId = oppositeId;
    }

    public Profile getSender() {
        return sender;
    }

    public void setSender(Profile sender) {
        this.sender = sender;
    }

    public CrossMessageType getType() {
        return CrossMessageType.valueOf(type);
    }

    public void setType(CrossMessageType type) {
        this.type = type.name();
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getLocalImage() {
        return localImage;
    }

    public void setLocalImage(String localImage) {
        this.localImage = localImage;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public boolean isMe() {
        return sender.getId() != oppositeId;
    }

    public CrossRoomInfo getCrossRoomInfo() {
        return crossRoomInfo;
    }

    public void setCrossRoomInfo(CrossRoomInfo crossRoomInfo) {
        this.crossRoomInfo = crossRoomInfo;
    }

    public int getRound() {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }

    public String getRealImage() {
        return localImage != null ? localImage : image;
    }

    public CrossQuestionInfo getCurrentQuestionInfo() {
        if (this.round == 0) {
            return crossRoomInfo.getFirst();
        } else {
            return crossRoomInfo.getSecond();
        }
    }

    public Profile getCurrentQuestionMan() {
        if (this.round == 0) {
            return crossRoomInfo.getFirst().getOwner();
        } else {
            return crossRoomInfo.getSecond().getOwner();
        }
    }

    public Profile getCurrentAnswerMan() {
        if (this.round == 0) {
            return crossRoomInfo.getSecond().getOwner();
        } else {
            return crossRoomInfo.getFirst().getOwner();
        }
    }

    public Profile getCurrentExposeMan() {
        if (getCurrentQuestionInfo().isAnswerRight()) {
            return getCurrentQuestionMan();
        } else {
            return getCurrentAnswerMan();
        }
    }

    public Profile getCurrentWelfareMan() {
        if (getCurrentQuestionInfo().isAnswerRight()) {
            return getCurrentAnswerMan();
        } else {
            return getCurrentQuestionMan();
        }
    }
}